﻿#include "LcdWatchdogApplication.h"

#include <QtCore/QDebug>
#include <QtCore/QJsonDocument>
#include <QtCore/QJsonArray>
#include <QtCore/QMetaEnum>
#include <QtCore/QMetaObject>
#include <QtCore/QSettings>

LcdWatchdogApplication::LcdWatchdogApplication(int argc, char *argv[])
	: QCoreApplication(argc,argv)
{
	firstLine=0;
	firstLineColor=0;
	exitCode=0;

	QSettings settings("LcdWatchdog.ini",QSettings::IniFormat);
	settings.beginGroup("LcdWatchdog");
	QString title = settings.value("title","Lcd Watchdog").toString();
	int refresh = settings.value("refresh",30000).toInt();
	serveurNames = settings.value("serveurNames").toStringList();
	serveurs = settings.value("serveur").toStringList();
	serveurImage = settings.value("serveurImage","").toString();
	settings.endGroup();

	if(serveurs.size()==0 && serveurImage.isEmpty())
		exitCode=-1;
	if(serveurImage.isEmpty())
		useServeurImage=false;
	else useServeurImage=true;

	lcd = new LogiLcd(title);
	//lcd->repeate=repeate;
	connect(lcd, SIGNAL(buttonPressed(int)), this, SLOT(buttonPressed(int)));

	nam = new QNetworkAccessManager(this);
	connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedSlot(QNetworkReply*)));

	timer = new QTimer();
	timer->setInterval(refresh);
	connect(timer, SIGNAL(timeout()), this, SLOT(timeout()));
	timer->start();

	timeout();
}

LcdWatchdogApplication::~LcdWatchdogApplication()
{
	shutdown();
}
void LcdWatchdogApplication::shutdown()
{
	if(lcd!=NULL)
	{
		disconnect(lcd, SIGNAL(buttonPressed(int)), this, SLOT(buttonPressed(int)));
		delete lcd;
	}
	lcd=NULL;
}

void LcdWatchdogApplication::timeout()
{
	if(!mutex.tryLock())
	{
		qDebug()<<"mutex.tryLock() fail ";
		return;
	}
	if(useServeurImage && !serveurImage.isEmpty())
	{
		QUrl url(serveurImage);
		QNetworkRequest request(url);
		request.setAttribute(QNetworkRequest::User,-1);
		QNetworkReply* reply = nam->get(request);
	}
	else
	{
		int i=0;
		for(QString serveur : serveurs)
		{

			QUrl url(serveur);
			QNetworkRequest request(url);
			request.setAttribute(QNetworkRequest::User,i);
			QNetworkReply* reply = nam->get(request);
			++i;
		}
	}
	mutex.unlock();
}

void LcdWatchdogApplication::buttonPressed(int button)
{
	if(button!=0)
	{
		if(button & LOGI_LCD_MONO_BUTTON_0)
		{
			mutex.lock();
			if(!useServeurImage && !serveurImage.isEmpty())
				useServeurImage=true;
			else if(useServeurImage && serveurs.size()>0)
				useServeurImage=false;
			lcd->ResetScreen();
			mutex.unlock();
			timeout();
		}
		else if(button & LOGI_LCD_MONO_BUTTON_1)
		{
		}
		if(button & LOGI_LCD_MONO_BUTTON_2)
		{
		}
		else if(button & LOGI_LCD_MONO_BUTTON_3)
		{
		}
	}
}

void LcdWatchdogApplication::finishedSlot(QNetworkReply* reply)
{
	if(!mutex.tryLock())
	{
		qDebug()<<"mutex.tryLock() fail ";
		return;
	}
	// Reading attributes of the reply
	// e.g. the HTTP status code
	QVariant statusCodeV = 
		reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
	// Or the target URL if it was a redirect:
	QVariant redirectionTargetUrl =
		reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
	// see CS001432 on how to handle this

	int index = reply->request().attribute (QNetworkRequest::User).toInt();
	QString message;
	if(index>0 && serveurNames.size()>index)
	{
		message+=serveurNames.at(index)+": ";
	}

	// no error received?
	if (reply->error() == QNetworkReply::NoError)
	{
		QByteArray bytes = reply->readAll();  // bytes
		if(index<0 && useServeurImage)
		{

			lcd->SetMonoBackgroundFromByteArray(bytes);
		}
		else if(!useServeurImage)
		{
			message+=bytes;
			lcd->SetMonoText(index,message);
		}
	}
	// Some http error received
	else
	{
		message+=QString("%1 %2").arg(statusCodeV.toInt()).arg(reply->errorString());
		lcd->SetMonoText(index,message);
	}

	// We receive ownership of the reply object
	// and therefore need to handle deletion.
	delete reply;
	mutex.unlock();
}